package softtech.apps.utility;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

/**
 * 
 * @author tdinhphuoc
 * Date create: 15-9-2014
 */
public class FileUtility {
	
	public static final String TAG = "FILE UTILITY";
	
	public void SaveContentFile(Context context,String fileName,String data){
		String noiDung = data;
		if(!fileName.contains(".txt")){
			fileName = fileName + ".txt";
		}
		try {
			FileOutputStream outPut=context.openFileOutput(fileName, Context.MODE_PRIVATE);
			byte[] epmang = noiDung.getBytes();
			Log.e(TAG , ""+ epmang);
			outPut.write(epmang);
			Log.e(TAG,"GHI FILE "+fileName +" THANH CONG");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String LoadContentFile(Context context,String fileName){
		String cache="";
		if(!fileName.contains(".txt")){
			fileName = fileName + ".txt";
		}
		FileInputStream inPut;
		try {

			inPut = context.openFileInput(fileName);

			BufferedReader reader = new BufferedReader(new InputStreamReader(inPut));
			try {
				String c =null;
				
				while((c=reader.readLine())!=null){
					cache+=c;
				}
			} catch (IOException e) {

				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}
		
		return cache;
	}
	
	/**
	 * 
	 * @param s
	 * @return
	 * true: not null
	 * false: null
	 */
	public static Boolean checkStringNotNull(String s ){
		return (s!=null && !s.equals(null) && s.length() >= 1 );
	}
	
	
	/**
	 * check string not null
	 * @param s
	 * @param minLength
	 * @return
	 * true: not null
	 * false: null
	 */
	public static Boolean checkStringNotNull(String s , int minLength){
		return (s!=null && !s.equals(null) && s.length() >=minLength );
	}
	
	/**
	 * Create folder if not exist
	 * @param PATH_APP
	 * @return
	 */
	public static boolean createDirIfNotExists(String PATH_APP) {
		Log.e(TAG , "TAO THU MUC");	    
	    final File dir = new File(PATH_APP);

        if (!dir.exists())
        {
        	
            dir.mkdirs(); 
            return false;
        }


	    return true;
	}
	
	/**
	 * Save content file to sdcard
	 * @param content
	 * @param path_folder
	 * @param file_name
	 */
	public void saveContentFileToSdCard(String content , String path_folder , String file_name){
		FileOutputStream fos = null;

        try {
            final File dir = new File(path_folder);

            if (!dir.exists())
            {
                dir.mkdirs(); 
            }

            final File myFile = new File(dir, file_name);

            if (!myFile.exists()) 
            {    
                myFile.createNewFile();
            } 

            fos = new FileOutputStream(myFile);

            fos.write(content.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
	
	/**
	 * Load content file from file in sdcard
	 * @param path_file
	 * @param file_name
	 * @return
	 */
	public String getContentFileFromSdcard(String path_file , String file_name){

		//Get the text file
		File file = new File(path_file,file_name);

		//Read text from file
		StringBuilder text = new StringBuilder();

		try {
		    BufferedReader br = new BufferedReader(new FileReader(file));
		    String line;

		    while ((line = br.readLine()) != null) {
		        text.append(line);
		        text.append('\n');
		    }
		    br.close();
		}
		catch (IOException e) {
		    //You'll need to add proper error handling here
			e.printStackTrace();
			return "";
		}
		
		return text.toString();
	}
	
}
